package com.starbucks.opencase.function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class funUserTest {
    @Autowired
    private funSession funSession;
    /**
     * 检测是否存在phone或uid
     * @param message
     * @return
     */
    public boolean testHasUIdOrPhone(String id, JsonNode message, ObjectNode on){
        if(!message.has(enumCMKey.phone.name()) && !message.has(enumCMKey.uid.name())){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_phone_or_uid.getTip());
            funSession.sendMessage(id,on);
            return false;
        }
        String phone="",uid="";
        if(message.has(enumCMKey.phone.name()))phone=message.get(enumCMKey.phone.name()).asText();
        if(message.has(enumCMKey.uid.name()))uid=message.get(enumCMKey.uid.name()).asText();
        if(phone.isEmpty() && uid.isEmpty()){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_phone_or_uid.getTip());
            funSession.sendMessage(id,on);
            return false;
        }
        return true;
    }
    public boolean hasUID(String id,JsonNode message,ObjectNode on){
        if(message.has(enumCMKey.uid.name()))return true;
        on.put(enumCMKey.tip.name(), enumCMTip.no_has_uid.getTip());
        funSession.sendMessage(id,on);
        return false;
    }
    public boolean hasPID(String id,JsonNode message,ObjectNode on){
        if(message.has(enumCMKey.pid.name()))return true;
        on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
        funSession.sendMessage(id,on);
        return false;
    }
    public boolean hasMID(String id,JsonNode message,ObjectNode on){
        if(message.has(enumCMKey.mid.name()))return true;
        on.put(enumCMKey.tip.name(), enumCMTip.no_has_uid.getTip());
        funSession.sendMessage(id,on);
        return false;
    }
    public boolean hasResult(String id,JsonNode message,ObjectNode on){
        if(message.has(enumCMKey.result.name()))return true;
        on.put(enumCMKey.tip.name(), enumCMTip.no_has_result.getTip());
        funSession.sendMessage(id,on);
        return false;
    }
    public boolean hasMessage(String id,JsonNode message,ObjectNode on){
        if(message.has(enumCMKey.message.name()))return true;
        on.put(enumCMKey.tip.name(), enumCMTip.no_has_message.getTip());
        funSession.sendMessage(id,on);
        return false;
    }
    /**
     * 获取手机号
     * @param message
     * @return
     */
    public String getPhone(JsonNode message){
        String phone="";
        if(message.has(enumCMKey.phone.name()))phone=message.get(enumCMKey.phone.name()).asText();
        return phone;
    }
    /**
     * 获取uid
     * @param message
     * @return
     */
    public String getUId(JsonNode message){
        String uid="";
        if(message.has(enumCMKey.uid.name()))uid=message.get(enumCMKey.uid.name()).asText();
        return uid;
    }
    public String getMId(JsonNode message){
        String uid="";
        if(message.has(enumCMKey.mid.name()))uid=message.get(enumCMKey.mid.name()).asText();
        return uid;
    }
    public boolean getResult(JsonNode message){
        boolean uid=false;
        if(message.has(enumCMKey.result.name()))uid=message.get(enumCMKey.result.name()).asBoolean();
        return uid;
    }
    public String getMessage(JsonNode message){
        String uid="";
        if(message.has(enumCMKey.message.name()))uid=message.get(enumCMKey.message.name()).asText();
        return uid;
    }
}
